package com.heima.freemarker.controller;


import com.heima.freemarker.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/hello")
public class HellController {


    @GetMapping
    public String testSend(Model model) {
        model.addAttribute("name", "小王");

        Student student = new Student();
        student.setName("二狗子");
        student.setAge(25);

        model.addAttribute("stu", student);
        return "01-basic";
    }


    @GetMapping("/modelAndView")
    public ModelAndView testSend2() {

        ModelAndView mov = new ModelAndView();

        mov.addObject("name", "小王");

        Student student = new Student();
        student.setName("二狗子");
        student.setAge(25);

        mov.addObject("stu", student);

        mov.setViewName("01-basic");
        return mov;
    }



    @GetMapping("/modelmap")
    public String testSend3(ModelMap modelMap) {
        modelMap.put("name", "小王");

        Student student = new Student();
        student.setName("二狗子");
        student.setAge(25);

        modelMap.put("stu", student);
        return "01-basic";
    }



    @GetMapping("/list")
    public  String testlist(Model model){
        Student student1 = new Student();
        student1.setName("二狗子");
        student1.setAge(25);
        student1.setMoney(100d);

        Student student2 = new Student();
        student2.setName("狗子");
        student2.setAge(25);
        student2.setMoney(100d);

        List stuList =new ArrayList();
        stuList.add(student1);
        stuList.add(student2);

        model.addAttribute("stuList",stuList);


        Map map=new HashMap();
        map.put("student1",student1);
        map.put("student2",student2);
        model.addAttribute("stuMap",map);

        return  "02-list";
    }

}
